class Sample:
    def __enter__(self):
        print("In __enter__()")
        return "Foo"
 
    def __exit__(self, type, value, trace):
        print("In __exit__()")
 
 
def get_sample():
    return Sample()
 

with get_sample() as sample:
    print ("sample:", sample)

# 1. enter()方法被执行
# 2. enter()方法返回的值 - 这个例子中是"Foo"，赋值给变量'sample'
# 3. 执行代码块，打印变量"sample"的值为 "Foo"
# 4. exit()方法被调用